/*
 * LibraryException.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.library;

import java.text.MessageFormat;

import android.content.Context;
import de.studiorutton.gerengdic.LearningWordsApplication;
import de.studiorutton.gerengdic.R;

public class LibraryException extends Exception {
	private static final long serialVersionUID = -1190287793495028361L;

	private String paramOne;
	private String paramTwo;
	private ErrorCode error;

	public enum ErrorCode {
		ERROR_UNKNOWN, ERROR_CANNOT_FIND_NTH_WORD
	};
	
	public LibraryException( Exception e ) {
		super( e );
	}

	public LibraryException(String string) {
		super( string );
	}
	
	public LibraryException( ErrorCode error ) {
		this.error = error;
	}

	public LibraryException(ErrorCode error, String param) {
		setErrorCodeAndParam(error, param);
	}
	
	public LibraryException(ErrorCode error, String param, String paramTwo) {
		setErrorCodeAndParams(error, param, paramTwo);
	}
	
	public void setErrorCodeAndParam(ErrorCode error, String param) {
		this.paramOne = param;
		this.error = error;
	}

	public void setErrorCodeAndParams(ErrorCode error, String paramOne,
			String paramTwo) {
		this.error = error;
		this.paramOne = paramOne;
		this.paramTwo = paramTwo;
	}

	@Override
	public String getLocalizedMessage() {
		
		Context c = LearningWordsApplication.get();
		
		int id = R.string.error_unknown;
		switch (error) {
		case ERROR_CANNOT_FIND_NTH_WORD:
			id = R.string.error_cannot_find_nth_word;
			break;
		
		default:
			id = R.string.error_unknown;
		}

		String msg = c.getResources().getString(id);
		String result = msg;
		if (paramTwo != null && paramOne != null) {
			result = MessageFormat.format(msg, paramOne, paramTwo );
		}
		else if (paramOne != null) {
			result = MessageFormat.format(msg, paramOne);
		} else
			result = msg;
		
		return result;
	}
}
